<?php


namespace app\api\controller;

use app\common\model\Member;
use app\common\model\Park as ParkModel;
use app\validate\ParkCheck;
use library\Controller;
use think\Db;
use think\Request;
class Park extends Base
{

    //车位
    public function add(Request $request,ParkCheck $check,ParkModel $Park){
        try {
            $param = $request->param();
            $result = $check->scene('park_check')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $msg = $Park->add($param);
                if ($msg==1) {
                    return $this->api_result(200, '请求成功', []);
                } else {
                    return $this->api_result(400, $msg, []);
                }
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }


    //车位修改
    public function update(Request $request,ParkModel $Park,ParkCheck $check){
        try {
            $param = $request->param();
            $result = $check->scene('park_save')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $Park->Park_save($param);
                if ($list==1) {
                    return $this->api_result(200, '请求成功', []);
                } else {
                    return $this->api_result(400, $list, []);
                }
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }

    //车位列表
    public function index(Request $request,ParkModel $Park,ParkCheck $check){
        try {
            $param = $request->param();
            $result = $check->scene('park_index')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $Park->index($param);
                if ($list) {
                    return $this->api_result(200, '请求成功', $list);
                } else {
                    return $this->api_result(400, $list, []);
                }
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }

    //车位删除
    public function del(Request $request,ParkModel $Park,ParkCheck $check){
        try {
            $param = $request->param();
            $result = $check->scene('park_del')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $Park->del($param);
                if ($list==1) {
                    return $this->api_result(200, '请求成功', []);
                } else {
                    return $this->api_result(400, $list, []);
                }
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }


    //电桩详情
    public function info(Request $request){
        $param = $request->param();
        $param['user_id'] = $param['user_id']??0;

        $find = Db::table("store_park")->where(['id'=>$param['id']])->find();
        $find['member'] = Db::table("store_member")->where(["id"=>$param['user_id']])->find();
        $is_collect = Db::table("store_member_collect")->where("type=2")->where(["table_id"=>$find['id']])->where(["user_id"=>$param['user_id']])->find();
        if($is_collect){
            $find['is_collect'] = 1;
        }else{
            $find['is_collect'] = 0;
        }

        $find['station'] = Db::table("store_power_station")->where(["id"=>$find['station_id']])->find();
        $find['images'] = str_replace('|',',',$find['images']);

        return $this->api_result(200, '请求成功', $find);
    }

    //车位收藏
    public function park_collect(Request $request,ParkModel $park,ParkCheck $check){
        $param = $request->param();
        $list = $park->collect($param);
        return $this->api_result(200, $list, $list);
    }

    //修改车位状态
    public function edit_status(){
        $id = input("id")??'';
        $user_id = input("user_id")??'';
        $status = input("status")??'1';
        Db::table("store_park")->where("id='$id'")->where("user_id='$user_id'")->update(['status'=>$status]);
        return $this->api_result(200, '请求成功', []);
    }



}